package www.controller;


import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import www.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HelloController {


    @RequestMapping("/auth")
    public String authTest(HttpServletResponse response){

        response.addHeader("Authorization", "Bearer123456789");

        return "auth";
    }

    @ResponseBody
    @RequestMapping(path = "/ip")
    public String sayip(String username){

        String ip = "10.101.5.111";
        return ip;
    }

    @RequestMapping(path = "/egou")
    public String paraA(String a){

        if (a == null){
            
        }
        System.out.println(a);
        return "success1";
    }


    @RequestMapping(path = "/hello")
    public String sayHello(int sex,int sex1,int sex2,int sex3,Model model){

        int a = sex+sex1+sex2+sex3;
        model.addAttribute("a",a);
        return "success1";
    }

    @RequestMapping(path = "/xss")
    public String xss(String a, Model model, HttpServletResponse response){

//        System.out.println(a);
        model.addAttribute("a",a);
        //response.setHeader("Access-Control-Allow-Origin","http://localhost:8080");
        return "success1";
    }





    @RequestMapping(path = "/el")
    public String tologin(){

        return "success";
    }

    @ResponseBody
    @RequestMapping("/ajax")
    public  Map<String,String> ajaxTest(){
        Map map = new HashMap();
        map.put("haha","123");
        map.put("hehe","123");
        return map;
    }

    @ResponseBody
    @RequestMapping("/jsonp")
    public  String testjsonp(){

        return "alert(1)";
    }


    @ResponseBody
    @RequestMapping("/body")
    public String bodytest(){

//        System.out.println(body);
//        Map map = new HashMap();
//        map.put("haha","123");
//        map.put("hehe","123");
        return "hahahahahaha";
    }


    //在使用redirect重定向时，必须要加上协议，否则默认是在本项目内进行重定向
    @RequestMapping("/urltest")
    public String urltest(String url){


        return "redirect:"+url;
    }




    //利用token解决了重放刷分的攻击
    @RequestMapping("/addgrade")
    public String tokentest(@RequestParam("token") String token,HttpServletRequest request){

        HttpSession session = request.getSession();

        System.out.println(session.getAttribute("token")+"session的");
        if (token != null){
            if(session.getAttribute("token").equals(token)){
                System.out.println("嘿嘿，你陈宫了");
                session.removeAttribute("token");
                if (session.getAttribute(token) == null){
                    System.out.println("已经删除token");
                }
                return "success7";
            }else {
                return "success6";
            }
        }else {
            return "success6";
        }
    }




    @RequestMapping("/write")
    public void write(HttpServletResponse response) throws Exception{
        response.getWriter().write("123");


    }



}
